﻿using System;
using System.Collections.Generic;
using System.Linq;
using AMM.Business.Project;
using AMM.Business.Store;
using AMM.CustomClassLibrary;
using AMM.CustomWinComponents.Validator;
using AMM.Model;
using AMM.Model.Project;
using AMM.Model.Store;

namespace AMM.UserControls {
    public partial class MouldShiftEditControl : BaseEditControl {

        private FlexBindingSource<MouldShift> _fbs;
        private MouldProcessor _mouldProcessor;

        public override object BaseEditModel {
            get { return this._fbs.CurrentItem; }
            set {
                if (this._fbs == null) { this._fbs = new FlexBindingSource<MouldShift>(); }
                this._fbs.CurrentItem = (MouldShift)value;
            }
        }

        public MouldShiftEditControl() {
            this.InitializeComponent();
        }

        private void MouldShiftEditControl_Load(object sender, EventArgs e) {
            if (this._fbs == null) { this._fbs = new FlexBindingSource<MouldShift>(); }
            this._mouldProcessor = new MouldProcessor();

            this.BindFromVendor();
            this.BindToVendor();
            this.BindMould();
            this.CreateFormBindings();
            this.BindValidation();
        }

        private void BindFromVendor() {
            VendorSearchModel searchModel = new VendorSearchModel() { IsEnabled = true };
            VendorProcessor vendorProcessor = new VendorProcessor(int.MaxValue);
            List<VendorViewModel> vendorList = vendorProcessor.FetchVendorViewModelList(searchModel).ToList();

            this.ddlFromVendor.DisplayMemberEx = "Code";
            this.ddlFromVendor.ValueMemberEx = "Key";
            this.ddlFromVendor.HeightEx = 300;
            this.ddlFromVendor.WidthEx = 400;
            this.ddlFromVendor.PageSizeEx = 20;
            this.ddlFromVendor.DataBind(new VendorViewModel(), x => new { x.Code, x.Name, x.Address }, vendorList);
        }

        private void BindToVendor() {
            VendorSearchModel searchModel = new VendorSearchModel() { IsEnabled = true };
            VendorProcessor vendorProcessor = new VendorProcessor(int.MaxValue);
            List<VendorViewModel> vendorList = vendorProcessor.FetchVendorViewModelList(searchModel).ToList();

            this.ddlToVendor.DisplayMemberEx = "Code";
            this.ddlToVendor.ValueMemberEx = "Key";
            this.ddlToVendor.HeightEx = 300;
            this.ddlToVendor.WidthEx = 400;
            this.ddlToVendor.PageSizeEx = 20;
            this.ddlToVendor.DataBind(new VendorViewModel(), x => new { x.Code, x.Name, x.Address }, vendorList);
        }

        private void BindMould() {
            MouldSearchModel searchModel = new MouldSearchModel();
            List<MouldViewModel> mouldList = this._mouldProcessor.FetchMouldViewModelList(searchModel).ToList();

            this.ddlMould.DisplayMemberEx = "Code";
            this.ddlMould.ValueMemberEx = "Key";
            this.ddlMould.HeightEx = 300;
            this.ddlMould.WidthEx = 400;
            this.ddlMould.PageSizeEx = 20;
            this.ddlMould.DataBind(new MouldViewModel(), x => new { x.Code, x.Name, x.MouldType }, mouldList);
        }

        private void CreateFormBindings() {
            this._fbs.CreateBinding(this.tbCode, x => x.Text, y => y.Code);
            this._fbs.CreateBinding(this.tbFromWho, x => x.Text, y => y.FromWho);
            this._fbs.CreateBinding(this.tbToWho, x => x.Text, y => y.ToWho);
            this._fbs.CreateBinding(this.tbFromWhy, x => x.Text, y => y.FromWhy);
            this._fbs.CreateBinding(this.dtpShiftDate, x => x.Value, y => y.ShiftDate);
            this._fbs.CreateBinding(this.ddlMould, x => x.SelectedValueEx, y => y.MouldId);
            this._fbs.CreateBinding(this.ddlFromVendor, x => x.SelectedValueEx, y => y.FromVendorId);
            this._fbs.CreateBinding(this.ddlToVendor, x => x.SelectedValueEx, y => y.ToVendorId);
        }

        private void BindValidation() {
            this.ValidatorForEdit.SetType(this.tbCode, ValidationType.Required | ValidationType.RegularExpression);
            this.ValidatorForEdit.SetRequiredMessage(this.tbCode, this.RequiredMessage);
            this.ValidatorForEdit.SetRegularExpression(this.tbCode, "^[A-Za-z0-9-]+$");
            this.ValidatorForEdit.SetRegularExpressionMessage(this.tbCode, "编码只能由字母，数字，连接线组成");

            this.ValidatorForEdit.SetType(this.tbFromWho, ValidationType.Required);
            this.ValidatorForEdit.SetRequiredMessage(this.tbFromWho, this.RequiredMessage);

            this.ValidatorForEdit.SetType(this.tbToWho, ValidationType.Required);
            this.ValidatorForEdit.SetRequiredMessage(this.tbToWho, this.RequiredMessage);

            this.ValidatorForEdit.SetType(this.tbFromWhy, ValidationType.Required);
            this.ValidatorForEdit.SetRequiredMessage(this.tbFromWhy, this.RequiredMessage);

            this.ValidatorForEdit.SetType(this.dtpShiftDate, ValidationType.Required);
            this.ValidatorForEdit.SetRequiredMessage(this.dtpShiftDate, this.RequiredMessage);

            this.ValidatorForEdit.SetType(this.ddlMould, ValidationType.Required);
            this.ValidatorForEdit.SetRequiredMessage(this.ddlMould, this.RequiredMessage);

            this.ValidatorForEdit.SetType(this.ddlFromVendor, ValidationType.Required);
            this.ValidatorForEdit.SetRequiredMessage(this.ddlFromVendor, this.RequiredMessage);

            this.ValidatorForEdit.SetType(this.ddlToVendor, ValidationType.Required);
            this.ValidatorForEdit.SetRequiredMessage(this.ddlToVendor, this.RequiredMessage);
        }

        private void ddlMould_ValueChanged(object sender, EventArgs e) {
            Mould mould = this._mouldProcessor.FetchMouldById(Convert.ToInt32(this.ddlMould.SelectedValueEx));
            this._fbs.CurrentItem.FromVendorId = mould.VendorId;
            this.ddlFromVendor.SelectedValueEx = mould.VendorId;
        }
    }
}
